function trim(findstr) {
    var myfind = findstr;
    for (var i = 0; i < findstr.length; i++) {
        var myfind = myfind.replace(" ", "");
    }
    return myfind;
}
function random_char(l) {
    var x = "0123456789qwertyuioplkjhgfdsazxcvbnm";
    var tmp = "";
    for (var i = 0; i < l; i++) {
        tmp += x.charAt(Math.ceil(Math.random() * 100000000) % x.length);
    }
    return tmp;
}

var get_width = function (obj) {
    var total_width;
    if (typeof obj == 'object') {
        total_width = obj.offsetWidth;
    } else {
        total_width = document.documentElement.clientWidth + document.documentElement.scrollLeft + document.body.scrollLeft;
    }
    return (total_width);
};

var get_height = function (obj) {
    var total_height;
    if (typeof obj == 'object') {
        total_height = obj.offsetHeight;
    } else {
        total_height = document.documentElement.clientHeight + document.documentElement.scrollTop + document.body.scrollTop;
    }
    return (total_height);
};

var get_dialog_status = function (o,p) {
	var val=new Object(),d;//dialog容器
	var dialog_obj=true;
	if (typeof o=='undefined')
		if (typeof $('#YY_mask').attr('content_id')!='undefined')
			d=$('#YY_dialog_ctn_'+$('#YY_mask').attr('content_id'));
		else
			dialog_obj=false;
	else
		d=$(o);
	if (dialog_obj){
		val.d_w=d.width();
		if (typeof d.css('padding-left')!='undefined')
			val.d_w+=parseInt(d.css('padding-left').replace(/px/, ""));
		if (typeof d.css('padding-right')!='undefined')
			val.d_w+=parseInt(d.css('padding-right').replace(/px/, ""));
		if (typeof d.css('border-left-width')!='undefined')
			val.d_w+=parseInt(d.css('border-left-width').replace(/px/, ""));
		if (typeof d.css('border-right-width')!='undefined')
			val.d_w+=parseInt(d.css('border-right-width').replace(/px/, ""));
		val.d_h=d.height();
		if (typeof d.css('padding-top')!='undefined')
			val.d_h+=parseInt(d.css('padding-top').replace(/px/, ""));
		if (typeof d.css('padding-bottom')!='undefined')
			val.d_h+=parseInt(d.css('padding-bottom').replace(/px/, ""));
		if (typeof d.css('border-top-width')!='undefined')
			val.d_h+=parseInt(d.css('border-top-width').replace(/px/, ""));
		if (typeof d.css('border-bottom-width')!='undefined')
			val.d_h+=parseInt(d.css('border-bottom-width').replace(/px/, ""));
	}
	val.st=$(document).scrollTop();
	val.sl=$(document).scrollLeft();
	val.w_h=$(window).height();
	val.w_w=$(window).width();
	if (typeof p=='undefined')
		return (val);
	else{
		eval('var v=val.'+p);
		return v;
	}
};
var YY_dialog_layer_index=1000;
function YY_dialog(params) {
    this.d_w_s = 400;
    this.d_h_s = 300;
    this.draggable = true;
    this.closable = true;
    this.content_id = null;
    this.focus_btn = '';
    this.package = true;
    this.restrict = true;
    this.force_center = false;
    this.dragger = null;
    this.pos_x = null;
    this.pos_y = null;
    this.w_w=function(){return $(window).width();};
    this.w_h=function(){return $(window).height();};
    this.d_w=0;
    this.d_h=0;

    this.config = function (params) {
        if (typeof (params) == "object") {
            if (typeof (params.content_id) != 'undefined' && params.content_id != '') {
                this.content_id = params.content_id;
                this.dialog_id = "YY_dialog_ctn_" + params.content_id;
                this.dialog_bg_id = "YY_dialog_bg_" + params.content_id;
            } else {
                random_content_id = random_char(5);
                this.content_id = random_content_id;
                this.dialog_id = "YY_dialog_ctn_" + random_content_id;
                this.dialog_bg_id = "YY_dialog_bg_" + random_content_id;
            }
            if (typeof (params.width) != 'undefined' && params.width != ''){
            	this.d_w_s = params.width; 
            }
            if (typeof (params.height) != 'undefined' && params.height != ''){
            	this.d_h_s = params.height; 
            }
            if (typeof (params.buttons) != 'undefined' && params.buttons != '') this.YY_dialog_buttons = params.buttons;
            if (typeof (params.draggable) == 'boolean') this.draggable = params.draggable;
            if (typeof (params.close) != 'undefined' && params.close != '') this.close = params.close;
            if (typeof (params.closable) == 'boolean') this.closable = params.closable;
            if (typeof (params.title) != 'undefined' && params.title != '') this.title = params.title;
            if (typeof (params.package) != 'undefined' && !params.package) this.package = false;
            if (typeof (params.dragger) != 'undefined' && params.dragger != null) this.dragger = params.dragger;
            if (typeof (params.pos_x) != 'undefined' && params.pos_x != null) this.pos_x = params.pos_x;
            if (typeof (params.pos_y) != 'undefined' && params.pos_y != null) this.pos_y = params.pos_y;
            if (typeof (params.restrict) != 'undefined' && !params.restrict) this.restrict = false;
            if (typeof (params.force_center) != 'undefined' && params.force_center) this.force_center = true;
        }
    };

    this.create_dialog = function () {
    	$('body').append('<div id="YY_dialog_ctn_'+this.content_id+'" content_id="'+this.content_id+'" class="YY_dialog_ctn"><div class="YY_dialog"></div></div>');
    	var d=$('#YY_dialog_ctn_'+this.content_id);//dialog容器
    	d.css({'position':'absolute','z-index':YY_dialog_layer_index,'display':'none','padding':'10px','background':'#c1c1c1'});
    	if (this.force_center) d.attr('position', 'center');
    	var c=d.children('div.YY_dialog');//dialog内容容器
    	c.css({'background':'#fff'});
        var o_s=get_dialog_status('#'+this.content_id);//对话框原始内容对象
        this.d_w=o_s.d_w;
        this.d_h=o_s.d_h;
        if (typeof (this.title) != 'undefined' && this.title != ''){
        	c.append('<h2>'+this.title+'</h2>');
        	if (this.draggable) {
        		c.children('h2').css({'cursor':'move','margin':'0','line-height':'30px'}).mousedown(down);
        	}
        	c.children('h2').css('cursor','move').get(0).onselectstart = new Function("return false");
        }
        if (this.closable){
        	c.append('<b></b>');
        	c.children('b').click(this.close_dialog).css({'position':'absolute','width':'16px','height':'16px','background':'#ddd','top':'15px','right':'15px','overflow':'hidden'});
            if (typeof (this.close) == 'function') c.children('b').get(0).close = this.close;       	
        }
        c.append('<div class="YY_dialog_content"></div>');
        c.children('.YY_dialog_content').css({'width':o_s.d_w,'height':o_s.d_h,'padding':'5px'});
        if ($('#'+this.content_id).length==1)c.find('.YY_dialog_content').append($('#'+this.content_id));
        $('#'+this.content_id).show();
        var d_s=get_dialog_status('#YY_dialog_ctn_'+this.content_id);
        this.d_w=d_s.d_w;
        this.d_h=d_s.d_h;
        if (typeof (params.buttons) == "object") {
        	c.append('<div class="action"></div>');
        	for (s in params.buttons) {
                if (params.buttons[s].name != '' && params.buttons[s].action != null) {
                	var btn_attr=' name="'+s+'"';
                	if (params.buttons[s].enter_key)btn_attr=' focus="true"';
                	c.children('.action').append('<button'+btn_attr+'>'+params.buttons[s].name+'</button>');
                	var btn_obj=c.children('.action').children('button:last');
                	//btn_obj.get(0).content_id = this.content_id;
                    if (params.buttons[s].action == "close")
                    	btn_obj.click(this.close_dialog);
                    else
                    	btn_obj.click(params.buttons[s].action);
                }
        	}
        }
        if (this.pos_x != null) {
        	c.css({'top':this.pos_x,'left':this.pos_y});
        } else {
        	var _top = Math.floor((this.w_h() - this.d_h) / 2);
        	var _left = Math.floor((this.w_w() - this.d_w) / 2);
        	_top=_top<0?0:_top;
        	_left=_left<0?0:_top;
        	d.css({'top':_top,'left':_left});
        }

    };

    this.create_ctn = function () {
    	$('body').append('<div id="YY_dialog_ctn_'+this.content_id+'" content_id="'+this.content_id+'" class="YY_dialog_ctn"><div class="YY_dialog"><div class="YY_dialog_content"></div></div></div>');
    	var d=$('#YY_dialog_ctn_'+this.content_id);
    	d.css({'position':'absolute','z-index':YY_dialog_layer_index,'display':'none'});
    	if (this.force_center) d.attr('position', 'center');
    	var c=d.children('div.YY_dialog');
    	var o=$('#'+this.content_id);
        var o_s=get_dialog_status('#'+this.content_id);//对话框原始内容对象
        this.d_w=o_s.d_w;
        this.d_h=o_s.d_h;
    	d.width(this.d_w).height(this.d_h);
    	c.children('.YY_dialog_content').append(o);
    	if (this.force_center) d.attr('position', 'center');
        $('#'+this.content_id).show();
    };

    this.create_mask = function () {
        if ($('#YY_mask').length==0) {
            $('body').append('<div id="YY_mask"></div>');
            $('#YY_mask').attr({'YY_dialog_id': "YY_dialog_ctn_" + this.content_id,'YY_dialog_content_id':this.content_id}).css({'top':0,'left':0,'display':'none','z-index':YY_dialog_layer_index-1,'background':'#000','position':'absolute','opacity':'0.4'});
        }
    };

    this.set_button = function (id, state, val) {
        if (typeof id == 'undefined' || id == '') return false;
        if (typeof state == 'undefined') state = true;
        obj = document.getElementById("YY_dialog_button_center_" + this.content_id + '_' + id);
        if (typeof val != 'undefined' && val != '') obj.innerHTML = val;
        if (!state) {
            obj.setAttribute('disabled', 'disabled');

        } else {
            obj.removeAttribute('disabled', 'disabled');

        }
    };
    this.show_action = function (action_str, type, loading) {
        if (typeof type == 'undefined') type = 'show';
        if (typeof loading == 'undefined') loading = true;
        if (type == 'hide') return false;
        var YY_dialog_action_ctn_layer = document.getElementById("YY_dialog_action_ctn_" + this.content_id);
        if (document.getElementById("YY_dialog_action_reply_" + this.content_id) == null) {
            var YY_dialog_action_reply_layer = document.createElement("div");
            YY_dialog_action_reply_layer.id = "YY_dialog_action_reply_" + this.content_id;

            YY_dialog_action_reply_layer.style.color = "#999";
            YY_dialog_action_reply_layer.style.height = 33 + "px";
            YY_dialog_action_reply_layer.style.lineHeight = 33 + "px";
            YY_dialog_action_reply_layer.style.styleFloat = "left";
            YY_dialog_action_reply_layer.style.cssFloat = "left";
            YY_dialog_action_reply_layer.style.paddingLeft = "35px";
            if (loading)
                YY_dialog_action_reply_layer.style.background = 'url("' + domain_source + '/images/loading_s.gif") no-repeat scroll 15px center #FFFFFF';
            else
                YY_dialog_action_reply_layer.style.background = '#FFFFFF';
            if (typeof action_str != 'undefined' && action_str != '') {
                YY_dialog_action_reply_layer.innerHTML = action_str;
            }
            YY_dialog_action_ctn_layer.appendChild(YY_dialog_action_reply_layer);
        } else {
            var YY_dialog_action_reply_layer = document.getElementById("YY_dialog_action_reply_" + this.content_id);
            if (type == 'show') {
                YY_dialog_action_reply_layer.style.display = 'block';
                if (loading)
                    YY_dialog_action_reply_layer.style.background = 'url("' + domain_source + '/images/loading_s.gif") no-repeat scroll 15px center #FFFFFF';
                else
                    YY_dialog_action_reply_layer.style.background = '#FFFFFF';
                if (typeof action_str != 'undefined' && action_str != '') {
                    YY_dialog_action_reply_layer.innerHTML = action_str;
                }
            } else {
                YY_dialog_action_reply_layer.style.display = 'none';
            }
        }
    };


    this.show_mask = function () {
    	var _st=$(document).scrollTop();
    	var _sl=$(document).scrollLeft();
    	$('#YY_mask').css({'width':this.w_w()+_sl,'height':this.w_h()+_st}).fadeIn(200);
    };
    

    this.show_dialog = function () {
        if (this.restrict) this.show_mask();
        if (this.pos_x == null) this.center_dialog();
        $("#YY_dialog_ctn_" + this.content_id).fadeIn(200);
        hide_select();
        if (this.focus_btn != '') $('#'+this.focus_btn).focus();
        if ($('#YY_mask').length>0)$('#YY_mask').attr('content_id',this.content_id);
    };

    this.center_dialog = function () {
    	var d=$('#YY_dialog_ctn_'+this.content_id);
    	var _st=$(document).scrollTop();
    	var _sl=$(document).scrollLeft();
        var _top = Math.floor(_st + (this.w_h() - this.d_h) / 2);
        var _left = Math.floor(_sl + (this.w_w() - this.d_w) / 2);
        d.css({'top':_top,'left':_left});
//        alert(_top+' st '+_st);
        
        //alert('left:'+_left+'\n top:'+_top+'\n st:'+_st+'\n sl:'+_sl+'\nww:'+this.w_w()+'\nwh:'+this.w_h()+'\nh:'+this.d_h);
    };
    this.close_dialog = function () {
    	var d_id;
    	if (typeof this.content_id=='undefined'){
    		d_id=$(this).parents('.YY_dialog_ctn').attr('content_id');
    	}else
    		d_id=this.content_id;
        if (typeof (this.close) == 'function') this.close();
        $('#YY_dialog_ctn_' + d_id).fadeOut();
        $('#YY_mask').fadeOut();
        hide_select(false);
    };

    hide_select = function (action) {
        if (action)
        	$('select[name!="dialog_select"]').hide();
        else
        	$('select[name!="dialog_select"]').show();
    };

    down = function (e) {
        var content_id = $(this).parents('.YY_dialog_ctn').attr('content_id');
        if (typeof content_id=='undefined')content_id=$('#YY_mask').attr('content_id');
        var d=$('#YY_dialog_ctn_'+content_id);
        d.css({'filter':"progid:DXImageTransform.Microsoft.Alpha(style=4,opacity=70)",'opacity':'0.7'});
        var event = window.event || e;
        tHeight = event.clientY- parseInt(d.css('top').replace(/px/, ""));
      	lWidth = event.clientX- parseInt(d.css('left').replace(/px/, ""));

        document.onmousemove = function (e) {
            var m=$('#YY_mask');
            var d_s=get_dialog_status('#YY_dialog_ctn_'+content_id);
            var event = window.event || e;
            if (d.css('left') == '')d.css('left',0);
            if (d.css('top') == '')d.css('top',0);
//            alert(event.clientY);
           
//            var all_width = get_width();
//            var all_height = get_height();

            if (m.length>0) {
            	m.css({'width':d_s.w_w+d_s.sl,'height':d_s.w_h+d_s.st});
            }

            var top = event.clientY - tHeight;
            var left = event.clientX - lWidth;

            top = top > d_s.w_h+d_s.st - d_s.d_h ? d_s.w_h+d_s.st - d_s.d_h : top;
            top = top <d_s.st ? d_s.st : top;
            left = left > d_s.w_w+d_s.sl - d_s.d_w? d_s.w_w+d_s.sl - d_s.d_w: left;
            left = left < d_s.sl ? d_s.sl : left;
            d.css({'left':left,'top':top});
        };
        document.onmouseup = function () {
        	document.onmousemove = null;
        	d.css({'filter':'','opacity':'1'});
        };
    };

    re_mask = function (e) {
    	var m=$('#YY_mask');
        var content_id = $(this).parents('.YY_dialog_ctn').attr('content_id');
        if (typeof content_id=='undefined')content_id=$('#YY_mask').attr('content_id');
        var d=$('#YY_dialog_ctn_'+content_id);
        var d_s=get_dialog_status('#YY_dialog_ctn_'+content_id);
        if (m.length>0) {
        	m.css({'width':d_s.w_w+d_s.sl,'height':d_s.w_h+d_s.st});
        }
		if (d.length>0&&d.attr('position') == 'center'){
            var top = parseInt((d_s.w_h - d_s.d_h)/2)+d_s.st;
            top = top <0 ? 0 : top;
            var left = parseInt((d_s.w_w - d_s.d_w)/2)+d_s.sl;
            left = left < 0 ? 0 : left;
            d.css({'left':left,'top':top});
            $('#ex_password').val(d_s.st+' '+d_s.sl+' '+d.css('top'));
		}
    };

    if (typeof (params) == "object") this.config(params);
    if (this.package)
        this.create_dialog();
    else
        this.create_ctn();

    if (this.dragger != null) {
        dragger_obj = $(this.dragger);
        dragger_obj.mousedown(down).attr('dialog_id',this.content_id).css('cursor','move');
        dragger_obj.get(0).onselectstart = new Function("return false");
    }
    if (this.restrict) {
        this.create_mask();
        window.onresize = re_mask;
        window.onscroll = re_mask;
    }
//    alert(get_dialog_status('#YY_dialog_ctn_max_exp_login_ctn','d_h'));
    if (this.focus_btn != '') {
        var focus_btn = this.focus_btn;

        document.onkeydown = function (evt) {
            var evt = window.event ? window.event : evt;
            if (evt.keyCode == 13) {
            	$('#'+focus_btn).click();
            }
        };
    }
}